import requests
import json
from time import sleep

from django.conf import settings


# from .exception import ValidationError


class MyRequests:
    def __init__(self, client_id):
        self.userName = 'admin'
        self.password = 'public'
        self.headers = {
            'content-type': 'application/json',
            'User-Agent': '0.0.1',
            "application/json;charset": "utf-8",
            "X-Access-Token": "eyJhbGciOiJIUzUxMiIsImlhdCI6MTY4OTMxNTI5OSwiZXhwIjoxNjg5NDAxNjk5fQ.eyJ1c2VybmFtZSI6ImFkbWluIn0.r7MHTGEyOfSi4iIsIRrHZWlSi7VVGMeJDVOz6PsYbWjo4GsEozTDROgIaWiQV7ewzGZQlTFLVVVyjqxfJlUsUQ"
        }

    def create(self, p_data):
        url_front = "http://120.36.152.254:21011/api/mfile/create/"
        r = requests.post(url_front, json=p_data, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
            is_file = content.get("is_file")
            if is_file:
                return True
            else:
                return False
        return False

    def list(self, p_data):
        url_front = "http://120.36.152.254:21011/api/mfile/list/"
        r = requests.get(url_front, params=p_data, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
        return False


m = MyRequests(0)
gatways = [
    ("TAC_V3.1_0002_B0025_des.bin", 1),
    ("TAC_V4.1_0001_B0002_des.bin", 1),
    ("TAC_V4.1_0003_B9007T2_des.bin", 1),
    ("dsprotocol-1.1.B00010_des.bin", 1),
    ("rtthread-4.0.2.B00015_des.bin", 1),
    ("TAC42_V4.1_0001_B0001T3_des.bin", 1),
    ("TAC42_V4.1_0001_B0001_des.bin", 1),
    ("TAC42_V4.1_0001_B9001T4_des.bin", 1),
    ("TACXX_V4.1_0001_B9001_des.bin", 1),
    ("TAC_V3.1_0002_B0026_des.bin", 1),
    ("TAC_V4.1_0002_B0006_des.bin", 1),
    ("TAC_V4.1_0003_B9007T4_des.bin", 1),
    ("rtthread-4.0.2.B00014_des.bin", 1)]
sensor = [
    ("TAA_V0.1_0001_B0001_des.bin", 2),
    ("TAA_V0.1_0002_B00034_des.bin", 2),
    ("TAA_V0.1_0003_B0045_des.bin", 2),
    ("TPA_V0.2_1002_B1043_des.bin", 2),
    ("TPA_V0.2_1002_B1044T7_des.bin", 2),
    ("TPA_V0.3_0022_B0381_des.bin", 2),
    ("TUH_V0.1_0019_B1057_des.bin", 2),
    ("TUH_V0.1_0019_B1058_des.bin", 2),
    ("TUH_V0.1_0019_B1059_des.bin", 2),
]
agreements = [
    ("83650315.config", 3),
    ("Protocol_B4015.bin", 3),
    ("Protocol_B4915.bin", 3),
]
configs = [
    ("72881021.config", 4),
    ("72881040.config", 4),
    ("TAC31-B0026-default.config", 4),
    ("TAC41-B0002-default.config", 4),
    ("TAC41-B0006-default.config", 4),
    ("TAC41-B0007-test2.config", 4),
]
datas = []
datas.extend(gatways)
datas.extend(sensor)
datas.extend(agreements)
datas.extend(configs)
# for data in datas:
#     data = {
#         "filename": data[0],
#         "filetype": data[1]
#     }
#     m.create(data)
#     sleep(1)
d = {
    "filetype": 1
}
m.list(d)
